package xcmg.device.service.safetyStock;

import java.math.BigDecimal;
import java.util.Date;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import xcmg.device.dao.entity.safetyStock.MaterialsSalesData;
import xcmg.device.service.vo.safetyStock.SafetyLevelInfo;

/**
 * 安全库存计算
 *
 * @author wangyong
 */
@Component
public class SafetyInventoryStrategy {

  private ApplicationContext applicationContext;
  @Autowired
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext=applicationContext;
  }

  /**
   * 计算出货量方差
   *
   * @param materialsSalesData
   * @param planCode
   * @return
   */
  public BigDecimal calSalesVariance(MaterialsSalesData materialsSalesData, String planCode) {
    SafetyInventoryService service = (SafetyInventoryService)applicationContext.getBean(ComSafetyInventoryEnum.getByOptionCode(planCode).getBeanId());
    return service.calSalesVariance(materialsSalesData);
  }

  /**
   * 计算安全系数相关数据
   *
   * @param orgId
   * @param materialsNo
   * @param now
   * @param planCode
   * @return
   */
  public SafetyLevelInfo getSafetyInfo(String orgId, String materialsNo, Date now, String planCode, BigDecimal deliverySpend)
      throws Exception {
    SafetyInventoryService service = (SafetyInventoryService)applicationContext.getBean(ComSafetyInventoryEnum.getByOptionCode(planCode).getBeanId());
    return service.getSafetyInfo(orgId, materialsNo, now, deliverySpend);
  }

  /**
   * 安全库存方案，无法简短描述其区别，以方案123...代替，页面选择给个解释
   */
  private enum ComSafetyInventoryEnum {
    OPTION_ONE("1", "safetyInventoryOptionOneService", "方案一"),
    OPTION_TWO("2", "safetyInventoryOptionTwoService", "方案二");
    private String optionCode;

    private String beanId;

    private String desc;


    static ComSafetyInventoryEnum getByOptionCode(String optionCode) {

      for (ComSafetyInventoryEnum safetyInventoryEnum : ComSafetyInventoryEnum.values()) {
        if (!safetyInventoryEnum.getOptionCode().equals(optionCode)) {
          continue;
        }
        return safetyInventoryEnum;
      }
      return OPTION_ONE;
    }

    ComSafetyInventoryEnum(String optionCode, String beanId, String desc) {
      this.optionCode = optionCode;
      this.beanId = beanId;
      this.desc = desc;
    }

    public String getOptionCode() {
      return optionCode;
    }

    public void setOptionCode(String optionCode) {
      this.optionCode = optionCode;
    }

    public String getBeanId() {
      return beanId;
    }

    public void setBeanId(String beanId) {
      this.beanId = beanId;
    }

    public String getDesc() {
      return desc;
    }

    public void setDesc(String desc) {
      this.desc = desc;
    }
  }


  interface SafetyInventoryService {

    /**
     * 计算近13个月出货量与均需求量之间的方差
     *
     * @param materialsSalesData
     * @return
     */
    BigDecimal calSalesVariance(MaterialsSalesData materialsSalesData);

    /**
     * 获取安全系数相关信息
     *
     * @param orgId
     * @param materialsNo
     * @param now
     * @return
     */
    SafetyLevelInfo getSafetyInfo(String orgId, String materialsNo, Date now, BigDecimal deliverySpend) throws Exception;
  }



}
